//
//  ChinaDetailViewController.m
//  GoodBuy
//
//  Created by dlios on 15-5-13.
//  Copyright (c) 2015年 GoodBuy. All rights reserved.
//

#import "ChinaDetailViewController.h"
#import "AFNetworking.h"
#import "UIImageView+WebCache.h"
#import "ChinaCommentViewController.h"
#import "YBBuyWebViewController.h"
#import "UMSocial.h"
#import "ChinaDCollectHandle.h"
@interface ChinaDetailViewController () <UMSocialUIDelegate>
@property (nonatomic, retain) UIButton *btn;
@property (nonatomic, retain) UIScrollView *scroll1;
@end

@implementation ChinaDetailViewController
- (void)dealloc
{
    [_article_mall release];
    [_UIVIEW release];
    [_juhua release];
    [_btn release];
    [_webView release];
    [_DetailArr release];
    [_article_title release];
    [_article_price release];
    [_article_pic release];
    [_article_filter_content release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"内在美";

    // 收藏
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"shoucang"] style:UIBarButtonItemStyleDone target:self action:@selector(collection)];
    
    [self getchuangjianshitu];
    [self getjuhua];
    
}
- (void)collection
{
    [[ChinaDCollectHandle shareChinaHandle] openDB];
    [[ChinaDCollectHandle shareChinaHandle] createTable];
    NSArray *array = [[ChinaDCollectHandle shareChinaHandle] selectChina];
    int flag = 0;
    for (ModelDetailChina *chinadetail in array) {
        if ([self.modelchina.article_id isEqualToString:chinadetail.article_id]) {
            flag = 1;
            break;
        }
    }
    if (flag) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"已经收藏过了呢." message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alert show];
    }else {
        [[ChinaDCollectHandle shareChinaHandle] insertChina:self.modelchina];
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"收藏成功" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alert show];
    }


}
- (void)getjuhua
{
    //小菊花
    self.juhua = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:_juhua];
    _juhua.labelText = @"玩命加载中";
    _juhua.tintColor = [UIColor cyanColor];
    
    [self.juhua show:YES];
    [self getdata];
    
}
- (void)getdata
{
    
    // -------------获取网络数据-----------
    
    NSString *url_string = [NSString stringWithFormat:@"http://api.smzdm.com/v1/youhui/articles/%@?f=iphone",self.article_id];
    NSLog(@"~~~~~~%@", url_string);
    
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        id dic = responseObject;
        id dic2 = [dic objectForKey:@"data"];
        //        NSLog(@"0000000000 %@", dic2);
        
        self.modelchina = [[ModelDetailChina alloc] initWithDic:dic2];
        
        [_DetailArr addObject:_modelchina];
        
        
        NSURL *pic = [NSURL URLWithString:_modelchina.article_pic];
        [_article_pic sd_setImageWithURL:pic];
        
        _article_price.text = _modelchina.article_price;
        _article_title.text = _modelchina.article_title;
        _article_mall.text = _modelchina.article_mall;
        [_webView loadHTMLString: _modelchina. article_filter_content baseURL:nil];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"失败==== %@",error);
        
    }];
}
-(void)getchuangjianshitu
{
    
    // 创建滑动视图
    self.scroll1 = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    
    _scroll1.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_scroll1];
    [_scroll1 release];

    // -------------商场
    
    self.article_mall = [[UILabel alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width / 1.12, 30, 30, [UIScreen mainScreen].bounds.size.height / 3.5)];
    [_article_mall setTextAlignment:NSTextAlignmentCenter];
    _article_mall.numberOfLines = 0;
    _article_mall.font = [UIFont systemFontOfSize:20];
    [_article_mall setTextColor:[UIColor grayColor]];
    [self.scroll1 addSubview:_article_mall];
    [_article_mall release];
    
    // -------------题目
    
    self.article_title = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    _article_title.frame = CGRectMake(10, 10, [UIScreen mainScreen].bounds.size.width - 20, [UIScreen mainScreen].bounds.size.height / 12.3);
    _article_title.numberOfLines = 0;
    [_article_title setTextAlignment:NSTextAlignmentCenter];
    _article_title.font = [UIFont systemFontOfSize:15];
    [self.scroll1 addSubview:_article_title];
    [_article_title release];
    
    // -------------图片
    
    self.article_pic = [[UIImageView alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width / 4, [UIScreen mainScreen].bounds.size.height / 10.8, [UIScreen mainScreen].bounds.size.width / 1.95, [UIScreen mainScreen].bounds.size.height / 5.5)];
    _article_pic.contentMode = 1;
    [self.scroll1 addSubview:_article_pic];
    [_article_pic release];
    
    // ------------价格
    
    self.article_price = [[UILabel alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width / 6, [UIScreen mainScreen].bounds.size.height / 3.5, [UIScreen mainScreen].bounds.size.width / 1.6, 40)];
    [_article_price setTextAlignment:NSTextAlignmentCenter];
    _article_price.numberOfLines = 0;
    [_article_price setTextColor:[UIColor redColor]];
    _article_price.font = [UIFont systemFontOfSize:15];
    [self.scroll1 addSubview:_article_price];
    [_article_price release];
    
    //--------------- webview
    
    self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height / 2.75, [UIScreen mainScreen].bounds.size.width, 370)];
    // 代理
    _webView.delegate = self;
    
    // 禁止webView 滑动
    self.webView.backgroundColor = [UIColor whiteColor];
    self.webView.scrollView.scrollEnabled = NO;
    [self.scroll1 addSubview:_webView];
    
    //----------下边新工具栏----------
    self.UIVIEW = [[UIView alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height - 49, [UIScreen mainScreen].bounds.size.width, 49)];
    _UIVIEW.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_UIVIEW];
    self.UIVIEW.alpha = 0.9;
    
    /**
     *  按钮
     */
    
    // -------------购买按钮
    
    self.btn = [UIButton buttonWithType:UIButtonTypeSystem];
    _btn.frame = CGRectMake(_UIVIEW.frame.size.width/2.2, 5, 40, 40);
    [_btn addTarget:self action:@selector(QQ) forControlEvents:UIControlEventTouchUpInside];
    [self.UIVIEW addSubview:_btn];
    [_btn setImage:[UIImage imageNamed:@"1"] forState:UIControlStateNormal];
    
    //---------------评论按钮
    
    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeSystem];
    btn1.frame = CGRectMake(_UIVIEW.frame.size.width/1.5 + 25, 5, 40, 40);
    [btn1 addTarget:self action:@selector(Conmment) forControlEvents:UIControlEventTouchUpInside];
    [self.UIVIEW addSubview:btn1];
    [btn1 setImage:[UIImage imageNamed:@"iconfont-pinglun-2"] forState:UIControlStateNormal];
    
    // ---------------分享按钮
    
    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeSystem];
    btn2.frame = CGRectMake(_UIVIEW.frame.size.width/4 - 25, 5, 40, 40);
    [btn2 addTarget:self action:@selector(Good) forControlEvents:UIControlEventTouchUpInside];
    [self.UIVIEW addSubview:btn2];
    [btn2 setImage:[UIImage imageNamed:@"iconfont-houtui"] forState:UIControlStateNormal];
    
}
// ------------------webview 自适应

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    NSString *height_str= [webView stringByEvaluatingJavaScriptFromString: @"document.body.offsetHeight"];
    int height = [height_str intValue];
    self.scroll1.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width,height+260);
    self.webView.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height / 2.75, [UIScreen mainScreen].bounds.size.width,  self.webView.scrollView.contentSize.height);
    [self.juhua hide:YES afterDelay:1];
    [self.juhua removeFromSuperview];
}

// -------------隐藏上页tabbar

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.hidesBottomBarWhenPushed=YES;
    }
    return self;
}


// -----------Button方法-------

- (void)QQ
{
    NSLog(@"%@", _modelchina.article_link);
    YBBuyWebViewController *buy = [[YBBuyWebViewController alloc] init];
    buy.webUrl = _modelchina.article_link;
    buy.titleStr = _modelchina.article_title;
    [self presentViewController:buy animated:YES completion:^{
        
    }];
    
}
- (void)Conmment
{
    ChinaCommentViewController *comment = [[ChinaCommentViewController alloc] init];
    comment.zuihou = _modelchina;
    [self.navigationController pushViewController: comment animated:YES];
}

- (void)Good
{
    NSString *str = [NSString stringWithFormat:@"#GoodBuy推荐#我正在看%@,这是个非常经典的商品,都来看看吧!%@", self.modelchina.article_title,self.modelchina.article_link];
    ;
    UIImageView *imageView = [[UIImageView alloc]init];
    [imageView sd_setImageWithURL:[NSURL URLWithString:self.chinalist.article_pic]];
    
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"5551988067e58eb48d00103b"
                                      shareText:str
                                     shareImage:imageView.image
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent,UMShareToRenren,UMShareToEmail,UMShareToFacebook,UMShareToTwitter,UMShareToSms,nil]
                                       delegate:self];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
